#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDateTime>
#include <QKeyEvent>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{
    QString strMessage = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss:zzz");
    strMessage += " keyPressEvent ";

    if (event->modifiers() & Qt::ControlModifier)
    {
        strMessage += " ctrl";
    }

    if (event->modifiers() & Qt::ShiftModifier)
    {
        strMessage += " shift";
    }

    if (event->modifiers() & Qt::AltModifier)
    {
        strMessage += " alt";
    }


    qDebug() << hex << event->key() << endl;

    if ( (0 ==  event->key()) || ( Qt::Key_unknown == event->key()) || event->key() > Qt::Key_Escape)
    {
        strMessage += " no_key";
    }else
    {
        strMessage += " key=";
        strMessage += QChar(event->key());
    }

    ui->statusBar->showMessage(strMessage);
}

void MainWindow::keyReleaseEvent(QKeyEvent *event)
{
    QString strMessage = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss:zzz");
    strMessage  += " keyReleaseEvent ";

    if (event->modifiers() & Qt::ControlModifier)
    {
        strMessage += " ctrl";
    }

    if (event->modifiers() & Qt::ShiftModifier)
    {
        strMessage += " shift";
    }

    if (event->modifiers() & Qt::AltModifier)
    {
        strMessage += " alt";
    }


    qDebug() << hex << event->key() << endl;

    if ( (0 ==  event->key()) || ( Qt::Key_unknown == event->key()) || event->key() > Qt::Key_Escape )
    {
        strMessage += " no_key";
    }else
    {
        strMessage += " key=";
        strMessage += QChar(event->key());
    }


    ui->statusBar->showMessage(strMessage);
}
